import machine
import time

# 定义两个 LED 连接的引脚
led_pin1 = machine.Pin(12, machine.Pin.OUT)
led_pin2 = machine.Pin(13, machine.Pin.OUT)

# 创建两个 PWM 对象
pwm_led1 = machine.PWM(led_pin1)
pwm_led2 = machine.PWM(led_pin2)

# 设置 PWM 频率
pwm_led1.freq(1000)
pwm_led2.freq(1000)

# 亮度步长
step = 5

# 定义按键引脚，假设按键连接到 GPIO 0
button_pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)

# 呼吸灯状态变量，True 表示运行，False 表示停止
breathing = True

# 上一次按键的状态
last_button_state = button_pin.value()

def breathing_effect():
    # LED1 渐亮，LED2 渐灭
    for i in range(0, 1024, step):
        pwm_led1.duty(i)
        pwm_led2.duty(1023 - i)
        time.sleep_ms(10)

    # LED1 渐灭，LED2 渐亮
    for i in range(0, 1024, step):
        pwm_led1.duty(1023 - i)
        pwm_led2.duty(i)
        time.sleep_ms(10)

while True:
    # 读取当前按键状态
    current_button_state = button_pin.value()

    # 检测按键是否按下（下降沿触发）
    if last_button_state == 1 and current_button_state == 0:
        # 切换呼吸灯状态
        breathing = not breathing
        if not breathing:
            # 停止呼吸灯，将两个 LED 亮度设为 0
            pwm_led1.duty(0)
            pwm_led2.duty(0)

    # 更新上一次按键状态
    last_button_state = current_button_state

    # 根据呼吸灯状态执行相应操作
    if breathing:
        breathing_effect()
    else:
        time.sleep(0.1)